package com.qfjy.service.Impl;

import cn.hutool.core.util.IdUtil;
import com.qfjy.entity.Publock;
import com.qfjy.mapper.PublockMapper;
import com.qfjy.service.PublockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class PublockServiceImpl implements PublockService {
    Random random=new Random();
    @Autowired
    private PublockMapper publockMapper;
    @Override
    public int insertSelective(Publock record) {
        Publock publock = this.selectYesterday(record.getUid()+"");
        if(selectByDate(record.getUid()+"")!=null){
            return 0;
        }
        if(publock==null){
          record.setId(random.nextInt());
            record.setSignScore(1);
            return publockMapper.insertSelective(record);
        }
        if(publock.getSignScore()>=4){

            record.setSignScore(4);

            return publockMapper.insertSelective(record);
        }
        record.setId(random.nextInt());
        record.setSignScore(publock.getSignScore()+1);
      return publockMapper.insertSelective(record);

    }

    /**
     * 查询本月打卡记录
     * @param
     * @return
     */
    @Override
    public List<Publock> selectSignList(String uid) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        return publockMapper.selectSignList(uid,sdf.format(new Date()));
    }

    @Override
    public Publock selectByDate(String uid) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Publock publock = publockMapper.selectSignByDate(uid,sdf.format(new Date()));
        return  publock;

    }

    /**
     * 查询前一天是否签到
     */

    public Publock  selectYesterday(String uid){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        Publock publock = publockMapper.selectYesterday(uid,sdf.format(new Date()));
        if(publock==null){
            return null;
        }
        return publock;
    }

    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

    }
}